package com.pc.service;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.pc.entity.Users;
import com.pc.mapper.UsersMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UsersService {

    @Resource
    private UsersMapper usersMapper;

    //登陆功能
    public boolean login(Users users){
        QueryWrapper<Users> wrapper = new QueryWrapper<>();
        wrapper.eq("username",users.getUsername()).eq("password",users.getPassword());
        Users user1 = usersMapper.selectOne(wrapper);
        return user1!=null;
    }

    //修改密码
    public boolean repwd(Users users){
        QueryWrapper<Users> wq = new QueryWrapper<>();
        wq.eq("username",users.getUsername());
        Users users1 = new Users();
        users1.setPassword(users.getPassword());
        int update = usersMapper.update(users1, wq);
        return update!=0;
    }

    //注册用户
    public boolean addUser(Users users){
        QueryWrapper<Users> wrapper = new QueryWrapper<>();
        wrapper.eq("username",users.getUsername());
        List<Users> users1 = usersMapper.selectList(wrapper);
        System.out.println(users1);
        if(users1.size()==0){
            int insert = usersMapper.insert(users);
            return insert!=0;
        }else
            return false;

    }

    //获得表中所有用户数量
    public List<Users> getUsers(){
        QueryWrapper<Users> wrapper = new QueryWrapper<>();
        return usersMapper.selectList(wrapper);
    }
}
